package com.blithe.wgt.http;



import com.blithe.wgt.http.converter.ToStringConverterFactory;
import com.blithe.wgt.http.gsonadapter.DoubleDefault0Adapter;
import com.blithe.wgt.http.gsonadapter.IntegerDefault0Adapter;
import com.blithe.wgt.http.gsonadapter.LongDefault0Adapter;
import com.blithe.wgt.http.gsonadapter.StringNullAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitServiceManager {
    private Retrofit mJsonRetrofit;
    private Retrofit mStringRetrofit;

    private static Gson gson;
    private static class SingleHolder {
        private static final RetrofitServiceManager INSTANCE = new RetrofitServiceManager();
    }

    public static RetrofitServiceManager getInstance() {
        return SingleHolder.INSTANCE;
    }

    public   RetrofitServiceManager () {
        mJsonRetrofit=createRetrofit(HttpConfig.BASE_URL_APP,FactoryEnum.JSON);
        mStringRetrofit=createRetrofit(HttpConfig.BASE_URL_APP,FactoryEnum.STRING);
    }


    private static Retrofit createRetrofit(String baseUrl,FactoryEnum factoryEnum){

        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(HttpConfig.DEFAULT_TIME_OUT, TimeUnit.SECONDS);//连接超时时间        builder.writeTimeout(DEFAULT_READ_TIME_OUT,TimeUnit.SECONDS);//写操作 超时时间
        builder.readTimeout(HttpConfig.DEFAULT_READ_TIME_OUT,TimeUnit.SECONDS);//读操作超时时间
        builder.addInterceptor(new MultipleBaseUrlInterceptor());
        // 创建Retrofit

        Converter.Factory   factory=null;
        switch (factoryEnum){
            case JSON:
                factory = GsonConverterFactory.create(buildGson());
                break;
            case STRING:
                factory = new ToStringConverterFactory();
                break;
            default:
                factory = GsonConverterFactory.create();

        }

        /*
         **打印retrofit信息部分
         */
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                //打印retrofit日志
                System.out.println("RetrofitLog---retrofitBack = "+message);
            }
        });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()//okhttp设置部分，此处还可再设置网络参数
                .addInterceptor(loggingInterceptor)
                .build();


        Retrofit  mRetrofit = new Retrofit.Builder()
                .client(builder.build())
                .baseUrl(baseUrl)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(factory)
                .build();


        return mRetrofit;

    }

    /**
     * 获取对应的Service
     * @param service Service 的 class
     * @param <T>
     * @return
     */
    public <T> T  createJsonService(Class< T >  service){

        return  mJsonRetrofit.create(service);
    }


    public <T> T createStringService(Class<T> service){
        return mStringRetrofit.create(service);
    }



    /**
     * 增加后台返回""和"null"的处理
     * 1.int=>0
     * 2.double=>0.00
     * 3.long=>0L
     *
     * @return
     */
    public static Gson buildGson(){
        if (gson == null) {
            gson = new GsonBuilder()
                    .registerTypeAdapter(Integer.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(int.class, new IntegerDefault0Adapter())
                    .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                    .registerTypeAdapter(Long.class, new LongDefault0Adapter())
                    .registerTypeAdapter(long.class, new LongDefault0Adapter())
                    .registerTypeAdapter(String.class, new StringNullAdapter())
                    .create();
        }
        return gson;
    }

}
